class Solution
{
public:
    long long maximumValueSum(vector<int> &nums, int k, vector<vector<int>> &edges)
    {
        vector<int> diff;
        long long result = 0;

        for (int num : nums)
        {
            result += num;
            diff.push_back((num ^ k) - num);
        }

        sort(diff.begin(), diff.end(), greater<int>());
        for (int i = 0; i < diff.size() - 1; i += 2)
        {
            if (diff[i] + diff[i + 1] > 0)
            {
                result += diff[i] + diff[i + 1];
            }
            else
            {
                break;
            }
        }

        return result;
    }
};